package com.atguigu.gulimall.gulimallware.listener;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.to.mq.OrderEntityTo;
import com.atguigu.common.to.mq.StockDetailTo;
import com.atguigu.common.to.mq.StockLockedTo;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.gulimallware.dao.WareSkuDao;
import com.atguigu.gulimall.gulimallware.entity.WareOrderTaskEntity;
import com.atguigu.gulimall.gulimallware.feign.OrderFeignService;
import com.atguigu.gulimall.gulimallware.service.WareOrderTaskService;
import com.atguigu.gulimall.gulimallware.service.WareSkuService;
import com.atguigu.gulimall.gulimallware.vo.OrderEntityVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@RabbitListener(queues = "stock.release.stock.queue")
@Service
public class StockReleaseListenner {
    @Autowired
    WareSkuService wareSkuService;
    @RabbitHandler
    public void handStockLockedRelease(StockLockedTo to, Message message, Channel channel) throws IOException {
        try {
            wareSkuService.unLockStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
    @RabbitHandler
    public void handOrderCloseRelease(OrderEntityTo to, Message message, Channel channel) throws IOException {
        try {
            wareSkuService.unlockStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
}
